package com.bestcem.xm.ticket.controller;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.ticket.controller.convert.wechat.TicketWechatConvert;
import com.bestcem.xm.ticket.controller.param.wechat.SubscribeNotificationParam;
import com.bestcem.xm.ticket.controller.vo.wechat.WechatUserInfoVO;
import com.bestcem.xm.ticket.dao.TicketWechatDao;
import com.bestcem.xm.ticket.dao.TicketWechatNotificationDao;
import com.bestcem.xm.ticket.entity.mysql.TicketWechatDO;
import com.bestcem.xm.ticket.service.TicketNotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Objects;

/**
 * 工单 微信相关接口
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version 通知提醒新增微信公众号
 * @date 2022/4/1 18:46
 */
@Slf4j
@RestController
@RequestMapping(value = "/ticket/wechat", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketWechatController {

    @Resource
    private TicketNotificationService ticketNotificationService;

    @Resource
    private TicketWechatDao ticketWechatDao;
    @Resource
    private TicketWechatNotificationDao ticketWechatNotificationDao;

    /**
     * 获取 微信用户信息
     * 通知提醒新增微信公众号需求新增
     *
     * @param wechatOpenId 微信openId
     * @return 微信用户信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 16:53
     */
    @AuthIgnore
    @GetMapping(value = "/userinfo/{wechatOpenId}")
    public XmResultJson<WechatUserInfoVO> getSubscriptionWechatUser(@PathVariable String wechatOpenId) {
        TicketWechatDO isDo = ticketWechatDao.findByOpenId(wechatOpenId);
        if (Objects.isNull(isDo)) {
            return XmResultJson.fail(ResourceErrorCodeEnum.NOT_FOUND, "微信用户信息不存在");
        }
        return XmResultJson.success(TicketWechatConvert.TicketWechatDOToVO(isDo));
    }


    /**
     * 微信用户订阅问卷通知
     * 通知提醒新增微信公众号需求新增
     *
     * @param param 订阅信息
     * @return void
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 13:32
     */
    @AuthIgnore
    @PostMapping(value = "/subscribe/notification")
    public XmResultJson<Boolean> subscribeNotification(@RequestBody SubscribeNotificationParam param) {
        if (!ObjectId.isValid(param.getSurveyId())) {
            return XmResultJson.fail(ResourceErrorCodeEnum.INVALID_FORMAT, "问卷id错误");
        }

        ServiceResult<Boolean> serviceResult = ticketNotificationService.createNotificationSubscriber(param.getSurveyId(), param.getWechatOpenId());
        if (serviceResult.isSuccess()) {
            return XmResultJson.success(serviceResult.getData());
        }
        return XmResultJson.fail(serviceResult.getErrorData(), serviceResult.getMsg());
    }


    /**
     * 微信用户退订问卷通知
     * 通知提醒新增微信公众号需求新增
     *
     * @param param 退订信息
     * @return void
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 13:33
     */
    @AuthIgnore
    @PostMapping(value = "/unsubscribe/notification")
    public XmResultJson<Object> unsubscribeNotification(@RequestBody SubscribeNotificationParam param) {
        if (!ObjectId.isValid(param.getSurveyId())) {
            return XmResultJson.fail(ResourceErrorCodeEnum.INVALID_FORMAT, "问卷id错误");
        }

        Boolean result = ticketWechatNotificationDao.removeNotificationSubscription(param.getSurveyId(), Collections.singletonList(param.getWechatOpenId()));
        if (!result) {
            return XmResultJson.fail(ResourceErrorCodeEnum.TOO_BUSY, "退订失败");
        }
        return XmResultJson.success(result);
    }
}
